﻿///////////////////////////////////////////////////////////////////////////////
//
// wxFormBuilder - A Visual Dialog Editor for wxWidgets.
// Copyright (C) 2005 José Antonio Hurtado
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
// Written by
//   José Antonio Hurtado - joseantonio.hurtado@gmail.com
//   Juan Antonio Ortega  - jortegalalmolda@gmail.com
//
///////////////////////////////////////////////////////////////////////////////

#ifndef __TYPE_UTILS__
#define __TYPE_UTILS__

//#include "types.h"
#include <QString>
#include <QPoint>
#include <QSize>
#include <QBitmap>
// macros para la conversión entre QString <-> QString
#define _WXSTR(x)  TypeConv::_StringToQString(x)
#define _STDSTR(x) TypeConv::_QStringToString(x)
#define _ANSISTR(x) TypeConv::_QStringToAnsiString(x)

namespace TypeConv
{
	QString _StringToQString( const std::string &str );
	QString _StringToQString( const char *str );
	std::string _QStringToString( const QString &str );
	std::string _QStringToAnsiString( const QString & str );

	QPoint StringToPoint( const QString &str );
	bool    StringToPoint( const QString &str, QPoint *point );
	QSize  StringToSize( const QString &str );

	QString PointToString( const QPoint &point );
	QString SizeToString( const QSize &size );

	int     BitlistToInt( const QString &str );
	int     GetMacroValue( const QString &str );
	int     StringToInt( const QString &str );

	bool     FlagSet( const QString &flag, const QString &currentValue );
	QString ClearFlag( const QString &flag, const QString &currentValue );
	QString SetFlag( const QString &flag, const QString &currentValue );

	QBitmap StringToBitmap( const QString& filename );

	//wxFontContainer StringToFont( const QString &str );
//	QString FontToString( const wxFontContainer &font );
//	QString FontFamilyToString(wxFontFamily family);
//	QString FontStyleToString(wxFontStyle style);
//	QString FontWeightToString(wxFontWeight weight);
    
	// wxColour GetColourFromStr(QString strValue);
	// wxColour StringToColour( const QString &str );
	// wxSystemColour StringToSystemColour( const QString& str );
//	QString ColourToString( const wxColour &colour );
    QString SystemColourToString( long colour );

	bool StringToBool( const QString &str );
	QString BoolToString( bool val );

	// wxArrayString StringToArrayString( const QString &str );
	// QString ArrayStringToString( const wxArrayString &arrayStr );

	void ParseBitmapWithResource( const QString& value, QString* image, QString* source, QSize* icoSize );

	/**
	@internal
	Used to import old projects.
	*/
	//wxArrayString OldStringToArrayString( const QString& str );

	QString ReplaceSynonymous( const QString &bitlist );

	void SplitFileSystemURL( const QString& url, QString* protocol, QString* path, QString* anchor );

	// Obtiene la ruta absoluta de un archivo
	QString MakeAbsolutePath( const QString& filename, const QString& basePath );
	QString MakeAbsoluteURL( const QString& url, const QString& basePath );

	// Obtiene la ruta relativa de un archivo
	QString MakeRelativePath( const QString& filename, const QString& basePath );
	QString MakeRelativeURL( const QString& url, const QString& basePath );

	// dada una cadena de caracteres obtiene otra transformando los caracteres
	// especiales denotados al estilo C ('\n' '\\' '\t')
	QString StringToText( const QString &str );
	QString TextToString( const QString &str );

	double StringToFloat( const QString& str );
	QString FloatToString( const double& val );
}


// No me gusta nada tener que usar variables globales o singletons
// pero hasta no dar con otro diseño más elegante seguiremos con este...
// TO-DO: incluirlo en GlobalApplicationData
class MacroDictionary;
typedef MacroDictionary* PMacroDictionary;

class MacroDictionary
{
private:
	typedef std::map<QString, int> MacroMap;
	static PMacroDictionary s_instance;

	typedef std::map<QString, QString> SynMap;

	MacroMap m_map;
	SynMap m_synMap;

	MacroDictionary();

public:
	static PMacroDictionary GetInstance();
	static void Destroy();
	bool SearchMacro( QString name, int *result );
	void AddMacro( QString name, int value );
	void AddSynonymous( QString synName, QString name );
	bool SearchSynonymous( QString synName, QString& result );
};

#endif //__TYPE_UTILS__

